package com.huanzhidadi.singleweb.requestlist;

import com.huanzhidadi.singleweb.requestlist.model.ItemVO;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 接口入参使用List<String>或String[]的实现方式
 *
 */
@RestController
@RequestMapping(value = "requestList")
public class RequestListController {

    /**
     * http://localhost:8080/requestList/requestParamArrayTest?stringArray=1&stringArray=2&stringArray=6
     * http://localhost:8080/requestList/requestParamArrayTest?stringArray=aaa,111,bnm
     * @param stringArray
     * @return
     */
    @RequestMapping(value = "requestParamArrayTest", method = RequestMethod.POST)
    public List<String> requestParamArrayTest(@RequestParam String[] stringArray) {
        return Arrays.asList(stringArray);
    }

    /**
     * http://localhost:8080/requestList/requestParamTest?stringList=1&stringList=2&stringList=6
     * http://localhost:8080/requestList/requestParamTest?stringList=aaa,111,bnm
     * @param stringList
     * @return
     */
    @RequestMapping(value = "requestParamTest", method = RequestMethod.POST)
    public List<String> requestParamTest(@RequestParam List<String> stringList) {
        return stringList;
    }

    /**
     * http://localhost:8080/requestList/requestBodyTest
     * application/json
     * ["1","2",5]
     * @param stringList
     * @return
     */
    @RequestMapping(value = "requestBodyTest", method = RequestMethod.POST)
    public List<String> requestBodyTest(@RequestBody List<String> stringList) {
        return stringList;
    }

    /**
     * http://localhost:8080/requestList/requestBodyArrayTest
     * application/json
     * ["1","2",5]
     * @param stringArray
     * @return
     */
    @RequestMapping(value = "requestBodyArrayTest", method = RequestMethod.POST)
    public List<String> requestBodyArrayTest(@RequestBody String[] stringArray) {
        return Arrays.asList(stringArray);
    }


    /**
     * http://localhost:8080/requestList/requestParamArray?shopId=aaa&itemIds=1&stringArray=2&itemIds=6
     * http://localhost:8080/requestList/requestParamArray?shopId=aaa&itemIds=aaa,111,bnm
     * @param itemIds
     * @return
     */
    @RequestMapping(value = "requestParamArray", method = RequestMethod.POST)
    public List<String> requestParamArray(@RequestParam(required = false) String shopId, @RequestParam String[] itemIds) {
        System.out.println("requestParamArray");
        for (String itemId : itemIds) {
            System.out.println(itemId);
        }
        return Arrays.asList(itemIds);
    }

    /**
     * http://localhost:8080/requestList/requestBodyArray?shopId=aaa&itemIds=1&stringArray=2&itemIds=6
     * http://localhost:8080/requestList/requestBodyArray?shopId=aaa&itemIds=aaa,111,bnm
     * @param itemVO
     * @return
     */
    @RequestMapping(value = "requestBodyArray", method = RequestMethod.POST)
    public List<String> requestBodyArray(@RequestBody ItemVO itemVO) {
        System.out.println("requestBodyArray");
        for (String itemId : itemVO.getItemIds()) {
            System.out.println(itemId);
        }
        return Arrays.asList(itemVO.getItemIds());
    }

}
